﻿using System;
using System.Windows.Forms;
using xClient.Framework;
using xClient.Framework.BaseView;
using xClient.Framework.PlugCore;

namespace cMES1009
{
    //站别捡料维护程序
    [Plug(ID = "cMES1009",
            Name = "站别捡料信息",
            Description = "站别捡料资料维护",
            Parent = "0,基础资料")]
    public partial class cMES1009 : StdView
    {
        public cMES1009()
        {
            InitializeComponent();
        }

        private void QueryGroupKPNList()
        {
            var _group = this.combGroupName.Text.Trim().ToUpper();
            var _kpnCode = this.combKPNCode.Text.Trim().ToUpper();
            ExecutionResult<dynamic> _res = new ExecutionResult<dynamic>();

            _res = Services.ServiceMethod.GetGroupKPNList(_group, _kpnCode);
            if (!_res.Status)
            {
                datalist.Rows.Clear();
                ShowMessage(_res.Message, MsgType.NG);
                return;
            }
            datalist.DataSource = _res.Data;
            datalist.ClearSelection();

        }

        private void InitialGroupAndCodeList()
        {
            var result = Services.ServiceMethod.GetGroupNameList();
            if (result.Status)
            {
                this.combGroupName.Items.Clear();
                foreach (var tmpGroup in result.Data)
                {
                    this.combGroupName.Items.Add(tmpGroup.group_name);
                }
            }

            var _kpnCode = this.combKPNCode.Text.ToUpper().Trim();
            result = Services.ServiceMethod.GetKPNCodeList(_kpnCode);
            if (result.Status)
            {
                this.combKPNCode.Items.Clear();
                foreach (var tmpCode in result.Data)
                {
                    this.combKPNCode.Items.Add(tmpCode.part_code);
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.QueryGroupKPNList();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmGroupKPNEdit _frmGpKPN = default(frmGroupKPNEdit);
            var clickBtnTag = ((ToolStripButton)sender).Tag;
            _frmGpKPN = new frmGroupKPNEdit(clickBtnTag.ToString());
            DialogResult _result = _frmGpKPN.ShowDialog();
            if (_result == DialogResult.OK)
            {
                this.ShowMessage("数据新增成功..", MsgType.OK);
                //refresh data
                this.QueryGroupKPNList();
            }
            else if (_result == DialogResult.No)
            {
                this.ShowMessage("数据新增失败..", MsgType.NG);
                //refresh data
                this.QueryGroupKPNList();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (datalist.SelectedRows.Count < 1)
            {
                this.ShowMessage("请选择一笔需要修改的资料.", MsgType.NG);
                return;
            }
            frmGroupKPNEdit _frmGpKPN = default(frmGroupKPNEdit);
            var clickBtnTag = ((ToolStripButton)sender).Tag;
            _frmGpKPN = new frmGroupKPNEdit(clickBtnTag.ToString(), datalist.SelectedRows[0]);
            DialogResult _result = _frmGpKPN.ShowDialog();
            if (_result == DialogResult.OK)
            {
                this.ShowMessage("数据修改成功..", MsgType.OK);
                //refresh data
                this.QueryGroupKPNList();
            }
            if (_result == DialogResult.No)
            {
                this.ShowMessage("数据修改失败..", MsgType.NG);
                //refresh data
                this.QueryGroupKPNList();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (datalist.SelectedRows.Count < 1)
            {
                this.ShowMessage("请选择一笔需要删除的资料..", MsgType.NG);
                return;
            }
            var _id = datalist.SelectedRows[0].Cells[0].Value;
        
            DialogResult dialog = MessageBox.Show(
                "确定要删除该资料吗?", "系统提示",
                MessageBoxButtons.YesNo,
                 MessageBoxIcon.Warning,
                  MessageBoxDefaultButton.Button2);
            if (dialog == DialogResult.Yes)
            {
                var result = Services.ServiceMethod.DeleteGroupKPNInfo(_id.ToString());
                if (result.Status)
                {
                    this.ShowMessage("删除数据成功..", MsgType.OK);
                    //刷新数据
                    this.QueryGroupKPNList();
                }
            }
        }

        private void datalist_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > 0)
            {
                frmGroupKPNEdit _frmGpKPN = default(frmGroupKPNEdit);
                _frmGpKPN = new frmGroupKPNEdit("EDIT", datalist.Rows[e.RowIndex]);
                DialogResult _result = _frmGpKPN.ShowDialog();
                if (_result == DialogResult.OK)
                {
                    this.ShowMessage("修改成功.", MsgType.OK);
                }
                else if (_result == DialogResult.No)
                {
                    this.ShowMessage("修改成功.", MsgType.OK);
                }
                this.QueryGroupKPNList();
            }
        }

        private void cMES1009_Load(object sender, EventArgs e)
        {
            this.QueryGroupKPNList();

            this.InitialGroupAndCodeList();
        }
    }
}
